package cn.changgou.pay.service.impl;

import cn.changgou.entity.HttpClient;
import cn.changgou.pay.service.WeixinPayService;
import com.github.wxpay.sdk.WXPayUtil;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

@Service
public class WeixinPayServiceImpl implements WeixinPayService {

    @Value("${weixin.appid}")
    private String appid;
    @Value("${weixin.partner}")
    private String partner;
    @Value("${weixin.partnerkey}")
    private String partnerkey;
    @Value("${weixin.notifyurl}")
    private String notifyurl;

    /***
     * 查询微信支付状态
     * */
    @Override
    public Map queryStatus(String outtradeno) {
        try {
            // 参数
            Map<String, String> paramMap = new HashMap<>();
            paramMap.put("appid", appid);
            paramMap.put("mch_id", partner);
            // 随机字符串
            paramMap.put("nonce_str", WXPayUtil.generateNonceStr());
            // 订单号
            paramMap.put("out_trade_no", outtradeno);
            // Map转为XML字符串，可以携带签名
            String xmlParameters = WXPayUtil.generateSignedXml(paramMap, partnerkey);
            // URL地址
            String url = "https://api.mch.weixin.qq.com/pay/orderquery";
            HttpClient httpClient = new HttpClient(url);
            // 提交方式
            httpClient.setHttps(true);
            // 提交参数
            httpClient.setXmlParam(xmlParameters);
            // 执行请求
            httpClient.post();
            // 获取返回的数据
            String result = httpClient.getContent();
            // 返回数据转为Map
            Map<String, String> resultMap = WXPayUtil.xmlToMap(result);
            return resultMap;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /***
     * 创建二维码
     * */
    @Override
    public Map createNative(Map<String, String> parameterMap) {
        try {
            // 参数
            Map<String, String> paramMap = new HashMap<>();
            paramMap.put("appid", appid);
            paramMap.put("mch_id", partner);
            // 随机字符串
            paramMap.put("nonce_str", WXPayUtil.generateNonceStr());
            paramMap.put("body", "畅购商城商品不错");
            // 订单号
            paramMap.put("out_trade_no", parameterMap.get("outtradeno"));
            // 交易金额，单位：分
            paramMap.put("total_fee", parameterMap.get("totalfee"));
            paramMap.put("spbill_create_ip", "127.0.0.1");
            // 交易结果回调通知地址
            paramMap.put("notify_url", notifyurl);
            paramMap.put("trade_type", "NATIVE");
            // Map转为XML字符串，可以携带签名
            String xmlParameters = WXPayUtil.generateSignedXml(paramMap, partnerkey);
            // URL地址
            String url = "https://api.mch.weixin.qq.com/pay/unifiedorder";
            HttpClient httpClient = new HttpClient(url);
            // 提交方式
            httpClient.setHttps(true);
            // 提交参数
            httpClient.setXmlParam(xmlParameters);
            // 执行请求
            httpClient.post();
            // 获取返回的数据
            String result = httpClient.getContent();
            // 返回数据转为Map
            Map<String, String> resultMap = WXPayUtil.xmlToMap(result);
            return resultMap;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}
